// import { generateRoutesFromMenu } from "@/utils/index";
//路由数据整理
function generateRoutesFromMenu(menu = []){    
    const ary = [];
    menu.forEach(routesParants => {
      if (routesParants.children) {
        if(routesParants.children.length !== 0){
            const routesChildren = [];
            routesParants.children.forEach(item=>{
                routesChildren.push({
                    path:'/'+item.route,
                    name:item.route,
                    component:item.route === 'home'?(resolve) => require([`@/views/${item.route}/index`], resolve):(resolve) => require([`@/views/${routesParants.route}/${item.route}/index`], resolve),
                    meta:{
                        title:item.name,
                        icon:item.icon
                    },
                })
                
            })
            ary.push({
                path:routesParants.route === '/'?'/':'/'+routesParants.route,
                name:routesParants.route,
                redirect:routesParants.route === '/'?'/home':'/'+routesParants.children[0].route,
                component:(resolve) => require([`@/layout`], resolve),
                meta:{
                    title:routesParants.name,
                    icon:routesParants.icon
                },
                children:routesChildren
            }) 
            
        }
      } else {
          alert('菜单数据错误');
          return false;
      }
    }) 
    // ary.push(
    //     { path: '*', redirect: '/404',hidden: true}
    // )
    return ary
  }
const state = {
    items:[]
  }
  const mutations = {
    ADD_MENU: (state,menuItems) => {
        if(menuItems.length == 0){
            state.items = [];
        }else{
            state.items = menuItems;
        }
    }   
  }
  
  const actions = {
    addMenu:async ({commit},menuItems) => {
        let menu = generateRoutesFromMenu(menuItems)
        commit('ADD_MENU',menu);  
        if(!sessionStorage.getItem('menuitems')){
            sessionStorage.setItem('menuitems',JSON.stringify(menuItems))          
        }     
    }
  }
export default {
    namespaced: true,
    state,
    mutations,
    actions
  }
